export var MediaCtrl = {
    video: null,
    stream: null,
    closeBtn: null,
    closeCb: null,
    canResolve: true,
    ratio: 0, // video宽高比例
    resizeTimer: -1,
    resizeCb: null,
    start: function() {
        var _this = this;
        _this.canResolve = true;
        _this.setMedia();

        return new Promise(function(resolve, reject) {
            try {
                navigator.mediaDevices.getUserMedia({
                    audio: false,
                    video: {
                        facingMode: 'user', // 优先使用前置摄像头
                        width: 400,
                        height: 400
                    }
                }).then(function(localMediaStream) {
                    if (!_this.canResolve) {
                        _this.stop(localMediaStream);
                        return;
                    }

                    _this.stream = localMediaStream;
                    document.querySelector('#localVideo').onloadedmetadata = function() {
                        document.querySelector('#localVideo').play();
                        _this.setVideoSize(true);
                        _this.addResizeListener();
                        resolve();
                    };

                    if ('srcObject' in document.querySelector('#localVideo')) {
                        document.querySelector('#localVideo').srcObject = localMediaStream;
                    } else {
                        document.querySelector('#localVideo').src = (window.URL && window.URL.createObjectURL(localMediaStream));
                    }
                }).catch(function(error) {
                    _this.stop();
                    reject(error);
                });
            } catch (error) {
                _this.stop();
                reject(error);
            }
        });
    },
    stop: function(stream) {
        try {
            this.stream.getTracks().forEach(function(track) {
                track.stop();
            });
        } catch (error) {}

        try {
            stream.getTracks().forEach(function(track) {
                track.stop();
            });
        } catch (error) {}

        this.canResolve = false;
        document.querySelector('#localVideo').pause();
        document.querySelector('#localVideo').srcObject = null;
        this.removeCloseBtn();
        this.removeMedia();
        this.removeResizeListener();
    },
    setMedia: function() {
        document.querySelector('.webrtc-demo').style.display = 'flex';
    },
    removeMedia: function() {
        document.querySelector('.webrtc-demo').style.display = '';
    },
    removeCloseBtn: function() {
        try {
            document.querySelector('.webrtc-demo').removeChild(this.closeBtn);
            this.closeBtn = this.closeCb = null;
        } catch (error) {}
    },
    listenClose: function(closeCb) {
        this.closeCb = closeCb;
    },
    setVideoSize: function(isFirst) {
        var _this = MediaCtrl;

        var _setVideoSize = function() {
            document.querySelector('#localVideo').width = parseFloat(window.getComputedStyle(document.querySelector('#localVideo')).width);

            if (_this.ratio) {
                document.querySelector('#localVideo').height = document.querySelector('#localVideo').width / _this.ratio;
            } else {
                document.querySelector('#localVideo').height = parseFloat(window.getComputedStyle(document.querySelector('#localVideo')).height);
                _this.ratio = document.querySelector('#localVideo').width / document.querySelector('#localVideo').height;
            }
            _this.resizeCb && _this.resizeCb();
        }

        if (isFirst == true) {
            _setVideoSize();
        } else {
            clearTimeout(_this.resizeTimer);
            _this.resizeTimer = setTimeout(_setVideoSize, 500);
        }
    },
    listenResize: function(resizeCb) {
        this.resizeCb = resizeCb;
    },
    addResizeListener: function() {
        window.addEventListener('resize', this.setVideoSize);
    },
    removeResizeListener: function() {
        clearTimeout(this.resizeTimer);

        this.resizeCb = null;
        this.ratio = 0;
        this.resizeTimer = -1;
        document.querySelector('#localVideo').removeAttribute('width');
        document.querySelector('#localVideo').removeAttribute('height');

        window.removeEventListener('resize', this.setVideoSize);
    }
};